package in.muton.edushare.components.forms.usersettings;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.common.EmailChecker;
import in.muton.edushare.components.error.ErrorPanel;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.Users;

public class EMailForm extends Panel {

	public EMailForm(String id, IModel<Users> aModel) {
		super(id, aModel);
		model = aModel;
		
		Form<Void> f = new Form<Void>("emailForm"){
			@Override
			public void onSubmit(){
				if(StringUtil.isValid(getEmail1()) && EmailChecker.isEmailId(getEmail1())){
					model.getObject().setEmail(getEmail1());
					EduShareSession.get().getSessionORMInterface().commitAllChanges();
					error = "Email id successfully changed.";
				}else{
					error = "Please provide a valid email id.";
				}
			}			
			private static final long serialVersionUID = 748855970435413356L;
		};
		
		add(f);
		f.add(new TextField<String>("primEmail",new PropertyModel<String>(this,"email1")));
		f.add(new ErrorPanel("fdbPanel",new PropertyModel<String>(this,"error")));
		setEmail1(model.getObject().getEmail());
	}
	
	private IModel<Users> model;
	private String email1 = "";
	private String error = null;
	
	public String getEmail1() {
		return email1;
	}

	public void setEmail1(String email1) {
		this.email1 = email1;
	}
	

	public void setError(String error) {
		this.error = error;
	}

	public String getError() {
		return error;
	}

	private static final long serialVersionUID = 611172862259232010L;
}
